<template>
  <div class="mt-1">
    <van-form>
      <h4 style="margin: 0 0 0.4rem 1rem; color: gray">
        当前产区:{{ this.$store.getters.productionLine }}
      </h4>
      <van-field
        v-model="code"
        clearable
        label="拍码"
        ref="pmfocus"
        @focus="stopKeyborad"
        placeholder="请输入拍码"
      />
      <ag-grid-vue
        style="width: 100%; height: 60vh"
        class="ag-theme-alpine mt-1"
        :columnDefs="columnDefs"
        :defaultColDef="defaultColDef"
        :gridOptions="gridOptions"
        :singleClickEdit="true"
        rowSelection="multiple"
        :rowData="rowData"
      >
      </ag-grid-vue>
      <audio src="../../assets/warning.mp3" preload="auto" id="bgMusic"></audio>
    </van-form>
  </div>
</template>

<script>
import { AgGridVue } from "ag-grid-vue";
import { CODE_LENGTH } from "../../config";
import { commissionSubmit } from "../../api/feeding";
import { mapGetters } from "vuex";
import { toast, parseQRCode } from "../../utils/common";
import { AgGrid } from "../../mixins/AgGrid";

export default {
  name: "StandardQuantity",
  components: { AgGridVue },
  mixins: [AgGrid],
  data() {
    return {
      code: "",
      fieldNames: {
        text: "name",
        value: "id",
      },
      columnDefs: [
        { headerName: "物料号", field: "ProductId", width: 90 },
        { headerName: "物料名称", field: "ProductName", width: 125 },
        { headerName: "生产日期", field: "ProductionDate", width: 100 },
        { headerName: "数量", field: "Num", width: 70 },
      ],
      rowData: [],
      material: null,
    };
  },
  watch: {
    code(val) {
      if (val.length > CODE_LENGTH) {
        let res = parseQRCode(val);
        if (res) {
          this.findPat(res.palletNo, res.materialCode);
          this.$refs.pmfocus.focus();
        } else {
          this.playMusic();
          toast("无法识别");
        }
      }
      this.code = "";
    },
  },
  computed: {
    ...mapGetters(["productionLine"]),
  },
  methods: {
    // 消除获取焦点时自动弹出键盘
    stopKeyborad() {
      this.$refs.pmfocus.$refs.input.setAttribute("readonly", "readonly");
      setTimeout(() => {
        this.$refs.pmfocus.$refs.input.removeAttribute("readonly");
      }, 200);
    },
    //播放警示音
    playMusic() {
      setTimeout(() => {
        let audio = document.getElementById("bgMusic");
        audio.play();
      }, 200);
    },
    //从外界添加数据
    async findPat(palletNo, materialCode) {
      if (!this.rowData.find((it) => it.UniqueId === palletNo)) {
        let user = this.$store.getters.user;
        let res = await commissionSubmit(
          this.productionLine,
          palletNo,
          materialCode,
          "4",
          user,
          "2",
          "",
          this.$store.getters.WarehouseId
        );
        if (res && res.Status == 200 && res.Data) {
          this.rowData.push({
            ProductName: res.Data.ProductName,
            ProductId: res.Data.ProductId,
            UniqueId: res.Data.UniqueId,
            Num: res.Data.Num,
            ProductionDate: res.Data.ProductionDate.substring(0, 10),
            OutStorageBillDetailId: res.Data.OutStorageBillDetailId,
          });
        } else {
          this.playMusic();
          toast(res.Msg);
        }
      } else {
        this.playMusic();
        toast("此拍已被录入");
      }
    },
  },
  mounted() {
    this.$refs.pmfocus.focus();
  },
};
</script>
